package com.ljf.code.domain.service;

import com.ljf.code.dto.other.TextConversionTranslation;
import com.ljf.code.enums.TextConversionTypeEnum;
import com.ljf.code.vo.TextConversionVO;
import com.ljf.common.domain.service.NoDBBasisDomainService;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.StringUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 文本转换  领域服务
 *
 * @author LJF
 * @since 2024-06-18
 */
@Component
public class TextConversionDomainService extends NoDBBasisDomainService {

    /**
     * 转化接口
     *
     * @param model
     * @return
     * @throws ServiceException
     */
    public List<TextConversionVO> translation(TextConversionTranslation model) throws ServiceException {
        TextConversionVO resultVo = null;
        if (Objects.equals(model.getCode(), TextConversionTypeEnum.SCREEN_RECORDING_CONVERSION_ABAP.getCode())) {
            resultVo = translationScreenRecordingConversionABAP(model);
        }
        List<TextConversionVO> resultList = new ArrayList<>();
        resultList.add(resultVo);
        return resultList;
    }

    /**
     * 转化--录屏转化ABAP
     *
     * @param model
     * @return
     * @throws ServiceException
     */
    private TextConversionVO translationScreenRecordingConversionABAP(TextConversionTranslation model) throws ServiceException {
        List<String> resultList = new ArrayList<>();
        for (String data : model.getDataList()) {
            if (StringUtil.isEmpty(data)) {
                continue;
            }
            if (data.charAt(0) != ' ') {
                if (CollectionUtil.isNotEmpty(resultList)) {
                    resultList.set(resultList.size() - 1, resultList.get(resultList.size() - 1) + '.');
                }
                resultList.add("PERFORM DYNPRO USING:");
                String[] split = data.trim().split("\t");
                resultList.add(String.format("'%s'\t'%s'\t\t'%s'", split[2], split[0], split[1]));
            }
            else {
                resultList.set(resultList.size() - 1, resultList.get(resultList.size() - 1) + ',');
                String[] split = data.trim().split("\t");
                resultList.add(String.format("' '\t'%s'\t\t'%s'", split[0], split[1]));
            }
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList.set(resultList.size() - 1, resultList.get(resultList.size() - 1) + '.');
        }
        TextConversionVO vo = new TextConversionVO();
        vo.setDataList(resultList);
        return vo;
    }

}
